<?php
namespace app\bis\controller;

class Deal extends Base
{
	//商品中心的团购列表页面
	public function index(){
		$deals = model('Deal')->getDeals(['status' => ['gt',0], 'bis_id' => $this->account->bis_id]);
		$deals = new \Page($deals,15);
		$this->assign('deals',$deals);
		return $this->fetch();
	}

	public function add(){
		$bisId = session('BisAccount','','bis')->bis_id;
		if(request()->isPost()){
			$data = input('param.');
			// var_dump(implode(',',$data['se_category_id']));die;
			//严格校验数据

			$deals = [
				'bis_id' => $bisId,
				'name' => $data['name'],
				'image' => $data['image'],
				'category_id' => empty($data['category_id']) ? '' : $data['category_id'],
				'se_category_id' => empty($data['se_category_id']) ? '' : implode(',',$data['se_category_id']),
				'city_id' => $data['city_id'],
				'location_ids' => empty($data['location_ids']) ? '' : implode(',',$data['location_ids']),
				'start_time' => strtotime($data['start_time']),
				'end_time' => strtotime($data['end_time']),
				'total_count' => $data['total_count'],
				'origin_price' => $data['origin_price'],
				'current_price' => $data['current_price'],
				'coupons_begin_time' => strtotime($data['coupons_begin_time']),
				'coupons_end_time' => strtotime($data['coupons_end_time']),
				'notes' => $data['notes'],
				'description' => $data['description'],
				'bis_account_id' => session('BisAccount','','bis')->id,
			];

			// var_dump($data['location_ids']);die;

			$dealId = model('deal')->add($deals);
			if($dealId){
				$this->success('添加成功',url('deal/index'));
			}else{
				$this->error('添加失败');
			}
		}
		$citys = model('city')->getCitysByParentId();
        $categorys = model('category')->getCategorysByParentId();
        $bisLocation = model('BisLocation')->getLocationByBisId($bisId);
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'bisLocation' => $bisLocation,
        ]);
		return $this->fetch();
	}
}
